#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include <iostream>
#include <string>
#include "log.hpp"

#include <mysql/mysql.h>

#include <jsoncpp/json/json.h>
#include <sstream>
#include <memory>

#include <fstream>

#include <websocketpp/server.hpp>
#include <websocketpp/config/asio.hpp>
typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

class mysql_util
{
public:
    static MYSQL *mysql_create(
        const std::string &host,
        const std::string &username,
        const std::string &password,
        const std::string &dbname,
        uint16_t port = 3306)
        {
            MYSQL *mysql = mysql_init(nullptr);
            if (mysql == nullptr)
            {
                ELOG("mysql init failed!");
                return nullptr;
            }

            if (mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0) == nullptr)
            {
                ELOG("mysql connect server failed! : %s", mysql_error(mysql));
                mysql_close(mysql);
                return nullptr;
            }

            if (mysql_set_character_set(mysql, "utf8") != 0)
            {
                ELOG("mysql set character failed! : %s", mysql_error(mysql));
                mysql_close(mysql);
                return nullptr;
            }

            return mysql;
        }
    
    static void mysql_destroy(MYSQL *mysql)
    {
        if (mysql == nullptr)
        {
            return;
        }

        mysql_close(mysql);
        return;
    }

    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        if (mysql_query(mysql, sql.c_str()) != 0) 
        {
            ELOG("SQL: %s", sql.c_str());
            ELOG("mysql query failed : %s\n", mysql_error(mysql));
            return false;
        }
        return true;
    }

};

class json_util
{
public:
    static bool serialize(const Json::Value &root, std::string& str)
    {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

        std::stringstream ss;
        int ret = sw->write(root, &ss);
        if (ret != 0)
        {
            ELOG("json serialize failed!!");
            return false;
        }
        str = ss.str();
        return true;
    }

    static bool unserialize(const std::string &str, Json::Value &root)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
        if (ret == false)
        {
            ELOG("json unserialize failed: %s", err.c_str());
            return false;
        }
        return true;
    }
};

class string_util
{
public:
    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res)
    {
        res.clear();
        size_t pos, idx = 0;
        while (idx < src.size())
        {
            pos = src.find(sep, idx);
            if (pos == std::string::npos)
            {
                res.push_back(src.substr(idx));
                break;
            }
            //123,123,,,,456连续的sep
            if (pos == idx)
            {
                idx += sep.size();
                continue;
            }

            res.push_back(src.substr(idx, pos-idx));
            idx = pos + sep.size();
        }
        return res.size();
    }
};

class file_util
{
public:
    static bool read(const std::string &filename, std::string &body)
    {
        std::ifstream ifs(filename, std::ios::binary);
        if (!ifs.is_open())
        {
            ELOG("%s file open failed!!", filename.c_str());
            return false;
        }

        size_t fsize = 0;
        ifs.seekg(0, std::ios::end);//将读取指针移动到文件末尾
        fsize = ifs.tellg();//tellg(),获取当前文件指针的位置，并返回从开头到文件指针的字节偏移量
        ifs.seekg(0, std::ios::beg);//将读取指针移动到文件开头
        body.resize(fsize);
        ifs.read(&body[0], fsize);//从文件中读取body.size()个字节到body中

        if (!ifs.good())//检查文件流在之前的操作中有没有出错
        {
            ELOG("read %s file content failed!", filename.c_str());
            ifs.close();
            return false;
        }

        ifs.close();
        return true;
    }
};
#endif